﻿// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Windows.Interop;

using System.Windows;

using ICSharpCode.Core;
using ICSharpCode.Core.WinForms;
using ICSharpCode.SharpDevelop.Gui;

namespace ICSharpCode.SharpDevelop.Gui
{
	/// <summary>
	/// Runs workbench initialization.
	/// Is called by ICSharpCode.SharpDevelop.Sda and should not be called manually!
	/// </summary>
    public class ShowMainFrm
	{
		const string workbenchMemento = "WorkbenchMemento";
        //App app;
		
		public void InitializeWorkbench()
		{
            //app = new App();



            CoreStartup startup = new CoreStartup("test");
            startup.ApplicationRootPath = AppDomain.CurrentDomain.BaseDirectory;
            startup.AllowUserAddIns = true;
            startup.AllowAddInConfigurationAndExternalAddIns = true;
            startup.ConfigDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
            startup.DataDirectory = startup.ConfigDirectory;
            startup.AddAddInsFromDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns"));



            startup.StartCoreServices();
            if (startup.AllowAddInConfigurationAndExternalAddIns)
            {
                startup.ConfigureExternalAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddIns.xml"));
            }
            if (startup.AllowUserAddIns)
            {
                startup.ConfigureUserAddIns(Path.Combine(PropertyService.ConfigDirectory, "AddInInstallTemp"),
                                            Path.Combine(PropertyService.ConfigDirectory, "AddIns"));
            }

            LoggingService.Info("Loading AddInTree...");
            startup.RunInitialization();

            System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();
            ComponentDispatcher.ThreadIdle -= ComponentDispatcher_ThreadIdle; // ensure we don't register twice
            ComponentDispatcher.ThreadIdle += ComponentDispatcher_ThreadIdle;            
			WorkbenchSingleton.InitializeWorkbench(new WpfWorkbench(), new AvalonDockLayout());
		}
		
		static void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
		{
			System.Windows.Forms.Application.RaiseIdle(e);
		}
		
		public void Run()
		{
            WorkbenchSingleton.MainWindow.Show();            
		}

        public void Close()
        {
            if (WorkbenchSingleton.MainWindow != null)
            {
                WorkbenchSingleton.MainWindow.Close();
            }
        }
        
	}
}
